#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_ntlauncher.h"
#include "imgprogressbar.h"
#include "manageform.h"
#include <QSystemTrayIcon>
#include "gameform.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include "customthread.h"
#include <QTimer>

enum QualityFlags
{
    Low = 0,                      //低画质 0
    Mid = 20,                            //中画质 20
    Height = 60,                        //高画质 60
};

class NtLauncher : public QMainWindow
{
    Q_OBJECT

public:
    NtLauncher(QWidget *parent = Q_NULLPTR);
    ~NtLauncher();

    void DoEnabled(bool Enabled);
    void MinWindowAction();

    void DoStartClient(QString NetIp, quint16 Port);
    QByteArray BuilderPack(QByteArray SourceByte);
    void DoSendDatas(QString Datas);
    void DoSendKeepBytes();
    void DoSendBytes(QByteArray Bytes, bool DoKey = true);
    void HandlerReceiveData(QByteArray IntactBytes);

private slots:
    void OnButtonMinClicked();
    void OnButtonCloseClicked();

    void SystemTrayIconClicked(QSystemTrayIcon::ActivationReason Activation);
    void ShowWindowAction();
    void ExitAction();

    void OnFunctionClicked();
    void OnOtherClicked();
    void OnPlayClicked();

    void AcceptMainConfig(QString Ip, quint16 Port);

    void OnTcpError(QAbstractSocket::SocketError ErrorCode);
    void OnTcpClose();
    void OnTcpConnect();
    void OnTcpReceive();

    void FuncSendTcpData(QString Data);

    void AcceptHttpError(QNetworkReply::NetworkError Error);
    void AcceptHttpProgress(qint64 NowLen, qint64 FileLen);
    void AcceptHttpDowloadFinished();
    void AcceptCheckMd5Yes();
    void AcceptNotOpenFile();
    void AcceptBeginHttpTimer();

    void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

    void DoHttpFinishedOrError();
    void BeginDowloadPatch();
    void BeginDowload(qint8 Index);
    void GameReadyNextPlay();
    void NowGameStart();

    void OnBtn1Clicked();
    void OnBtn2Clicked();
    void OnBtn3Clicked();
    void OnBtn4Clicked();
    bool eventFilter(QObject *Watched, QEvent *event) Q_DECL_OVERRIDE;


    void on_timer_out();

    void on_radioButton_clicked();

    void on_radioButton_2_clicked();

    void on_radioButton_3_clicked();

    void on_radioButton_4_clicked();

private:
    Ui::NtLauncherClass ui;

    int KeepAliveTimerId;
    int ResetPackTimerId;

    int TimerIndex;

    QString SerIp;                //服务器地址
    quint16 SerPort;            //服务器端口

    bool MPressFlag;
    QPoint BeginDrag;

    QSystemTrayIcon* TrayIcon;
    ManageForm* MForm;
    GameForm* GForm;

    QTcpSocket* PacketTcpClient;
    QByteArray AsDataBuff;                   //缓冲区用于缓存TCP包

    InitThread* ThreadInit;

    WorkObject* WObject;
    QThread* WorkThread;
    int HttpTimerId;                    //下载计时器 以此来计算每秒的速率
    quint64 NowRecvOn;           //下载过程收到的数据长度
    int OnSecs;                           //下载关联计时器的秒数
    QString StrSpeed;                //下载速率转换字符串
    quint8 DowCount;                  //下载的标记位

    QualityFlags InitQuality;            //图像质量
    bool LanKick;

    QTimer *m_timer;
    int curPix;
};
